package com.lhd.common.utils;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtil {

    private static JedisPool jedisPool;

    private static void initJedisPool() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(100);
        poolConfig.setMaxIdle(5);
        poolConfig.setMinIdle(5);
        poolConfig.setBlockWhenExhausted(true);
        poolConfig.setMaxWaitMillis(2000);
        poolConfig.setTestOnBorrow(true);
        jedisPool = new JedisPool(poolConfig, "hadoop102", 6379, 10000);
    }

    public static Jedis getJedis() {
        Jedis  jedis;
        if (jedisPool == null) {
            initJedisPool();
        }

        return  jedisPool.getResource();
    }


    public  static   void   insertRedis(String tableName, String  id, JSONObject jsonObject){
        Jedis jedis = JedisUtil.getJedis(); //创建jdbc链接、
        String redisKey = "DIM:" + tableName + ":" + id;
        jedis.set(redisKey, jsonObject.toJSONString());
//        //设置过期时间
        jedis.expire(redisKey, 24 * 60 * 60);
//        //归还连接
        jedis.close();
    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        String pong = jedis.ping();
        System.out.println(pong);
    }

}
